############################################################################
# CMakeLists.txt
# Copyright (C) 2016-2018  Belledonne Communications, Grenoble France
#
############################################################################
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
############################################################################

cmake_minimum_required(VERSION 3.22)

project(BV16 VERSION 2.1 LANGUAGES C)

include(GNUInstallDirs)

include_directories(${CMAKE_BINARY_DIR} ./bv16)
include_directories(${CMAKE_BINARY_DIR} ./bvcommon)

option(ENABLE_TOOLS "Build tools." NO)


find_library(LIBM NAMES m)


set(BV16_SOURCE_FILES
	bv16/bitpack.c
	bv16/coarptch.c
	bv16/decoder.c
	bv16/encoder.c
	bv16/excdec.c
	bv16/excquan.c
	bv16/fineptch.c
	bv16/gaindec.c
	bv16/gainquan.c
	bv16/levelest.c
	bv16/lspdec.c
	bv16/lspquan.c
	bv16/plc.c
	bv16/postfilt.c
	bv16/ptquan.c
	bv16/tables.c
	bvcommon/a2lsp.c
	bvcommon/allpole.c
	bvcommon/allzero.c
	bvcommon/autocor.c
	bvcommon/cmtables.c
	bvcommon/levdur.c
	bvcommon/lsp2a.c
	bvcommon/ptdec.c
	bvcommon/stblchck.c
	bvcommon/stblzlsp.c
	bvcommon/utility.c
)
set(BROADVOICE16_SOURCE_FILES
	bv16/bv.c
	bv16/g192.c
)

# Levinson method conflicts with AMRWB, so renaming this one
add_compile_definitions("Levinson=bv16_Levinson")

add_compile_definitions("WMOPS=0" "G192BITSTREAM=0")


if(MSVC)
	list(APPEND BV16_SOURCE_FILES bv16.def)
	set_source_files_properties(bv16/g192.c PROPERTIES COMPILE_OPTIONS "/wd4267") # Disable "possible loss of data" warning
endif(MSVC)

set(LIBS )
if(LIBM)
	list(APPEND LIBS ${LIBM})
endif()

add_library(bv16 ${BV16_SOURCE_FILES})
target_link_libraries(bv16 ${LIBS})
target_include_directories(bv16 PUBLIC
	$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
	$<INSTALL_INTERFACE:include>
)
if(NOT BUILD_SHARED_LIBS)
	set_target_properties(bv16 PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()

install(TARGETS bv16
	RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
	LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
	ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
	PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
)

if(MSVC AND BUILD_SHARED_LIBS)
	install(FILES $<TARGET_PDB_FILE:bv16>
		DESTINATION ${CMAKE_INSTALL_BINDIR}
		PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
		CONFIGURATIONS Debug RelWithDebInfo
	)
endif()

if(ENABLE_TOOLS)
	add_executable(bv16-exe ${BROADVOICE16_SOURCE_FILES})
	set_target_properties(bv16-exe PROPERTIES OUTPUT_NAME broadvoice16)
	target_link_libraries(bv16-exe bv16)
	if (NOT IOS)
		install(TARGETS bv16-exe
			RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
			LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
			ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
			PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
		)
	endif()
endif()

file(GLOB BV16_HEADER_FILES "bv16/*.h")
install(FILES ${BV16_HEADER_FILES}
	DESTINATION include/bv16-floatingpoint/bv16
	PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
)

file(GLOB BV16_COMMON_HEADER_FILES "bvcommon/*.h")
install(FILES ${BV16_COMMON_HEADER_FILES}
	DESTINATION include/bv16-floatingpoint/bvcommon
	PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
)

install(TARGETS bv16 EXPORT BV16Targets
	RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
	LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
	ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
	FRAMEWORK DESTINATION Frameworks
	PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
)

install(EXPORT BV16Targets
	FILE BV16Targets.cmake
	DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake"
)

add_subdirectory(build)
